package com.kc.sjwh.service;

import java.util.Date;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.kc.sjwh.dao.JwtsbxxDao;
import com.kc.sjwh.dao.CzllzDao;
import com.kc.sjwh.dao.CzllzcyDao;
import com.kc.sjwh.entity.po.Jwtsbxx;
import com.kc.sjwh.other.Page;
import com.kc.sjwh.other.QueryParam;
import com.kc.sjwh.other.Tool;

/**
 * 处置力量页 service
 * @author xianding
 * @date 2017年11月28日
 */
@Service
public class JwtsbxxService {
	static Logger logger = Logger.getLogger(JwtsbxxService.class);
	@Autowired
	JwtsbxxDao jwtsbxxDao;
	@Autowired
	CzllzDao czllzDao;
	@Autowired
	CzllzcyDao czllzcyDao;

	/**
	 * 分页查询
	 * @param pageSize
	 * @param pageNum
	 * @param param
	 * @return
	 */
	public Page<Jwtsbxx> getPage(int pageSize, int pageNum, Map<String, String> param) {
		QueryParam q = new QueryParam(pageSize, pageNum).setOrder("t.gxsj desc");
		
		//添加动态的查询条件
		String v = null;
		v = param.get("startTime");
		if(StringUtils.isNotEmpty(v)) {
			q.addSqlCondition(" t.gxsj >= to_date('"+v+"','yyyy-mm-dd hh24:mi')");
		}
		v = param.get("endTime");
		if(StringUtils.isNotEmpty(v)) {
			q.addSqlCondition(" t.gxsj <= to_date('"+v+"','yyyy-mm-dd hh24:mi')");
		}
		v = param.get("keyword");
		if(StringUtils.isNotEmpty(v)) {
			q.addSqlCondition("t.kbh like ? or t.jwthm like ? or t.sbbh like ? ".replace("?", "'%"+v+"%'"));
		}
		v = param.get("dwdm");
		if(StringUtils.isNotEmpty(v)) {
			q.put("t.ssdwdm", v);
		}
		
		return jwtsbxxDao.getPage("get", q);
	}

	/**
	 * 新增
	 * @param one
	 */
	public void add(Jwtsbxx one) {
		Jwtsbxx chk_n=jwtsbxxDao.getOne("getN",new QueryParam().put("t.jwthm", one.getJwthm()));
		if (chk_n!=null) {				
			jwtsbxxDao.delete(chk_n.getBh());			
		}
		Jwtsbxx chk=jwtsbxxDao.getOne(new QueryParam().put("t.jwthm", one.getJwthm()));
		if (chk!=null) {				
			throw new IllegalArgumentException("此警务通号码已经存在");			
		}
		one.setBh(Tool.getUuid());
		one.setCjsj(new Date());
		jwtsbxxDao.add(one);			

	}

	/**
	 * 修改
	 * @param one
	 */
	public void update(Jwtsbxx one) {
		Jwtsbxx chk_n=jwtsbxxDao.getOne("getN",new QueryParam().put("t.jwthm", one.getJwthm()));
		if (chk_n!=null) {				
			jwtsbxxDao.delete(chk_n.getBh());			
		}
		Jwtsbxx chk=jwtsbxxDao.getOne(new QueryParam().put("t.jwthm", one.getJwthm()));
		if (chk!=null&&(chk.getBh().equals(one.getBh()))) {				
			throw new IllegalArgumentException("此警务通号码已经存在");			
		}
		one.setGxsj(new Date());
		jwtsbxxDao.update(one, "bh");			
		

	}

	/**
	 * 删除
	 * @param id
	 */
	@Transactional
	public void delete(String id) {
		//级联删除 none
		Jwtsbxx one =new Jwtsbxx();
		one.setBh(id);
		one.setSybz("N");
		one.setGxsj(new Date());
		jwtsbxxDao.update(one, "bh");
//		//删除
//		int n = jwtsbxxDao.delete(id);
//		if(n == 0) {
//			throw new IllegalArgumentException("要删除的记录不存在");
//		}
	}

	/**
	 * 打开表单
	 * @param id
	 * @return
	 */
	public Jwtsbxx form(String id) {
		return id==null ? null : jwtsbxxDao.getOne(new QueryParam().put("bh", id));
	}

	/**
	 * 根据行设置处置力量页
	 */
//	public void setJwtsbxxByRow(HSSFRow row, Jwtsbxx one) {
//		
//	}

}
